package com.hmjk.health.views;

import java.math.RoundingMode;
import java.text.DecimalFormat;

import com.hmjk.health.R;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.WindowManager;



public class GluCakyView extends View{
	

	private static int BLUE = 0xff4bd1fe;
    private static int GREEN = 0xff91c869;	
    private static int LIGHT_GREEN = 0xffe4e86f;	
    private static int YELLOW = 0xffe4ce3c;	
    private static int DEEP_YELLOW = 0xfff5a93c;	
    private static int RED = 0xfff94244;
	private int mtextColor = 0xff444444;
	
    private int total = 0;    
    
    private int values[]= {0,0,0,0,0,0};
    private int colors[] = {GREEN,LIGHT_GREEN,YELLOW,DEEP_YELLOW,RED,BLUE};
    private float arcs[] = {0.0f,0.0f,0.0f,0.0f,0.0f,0.0f};

    
	private Context mContext;
	private int sysWidth,sysHeight;
	
	public void setParameters(int... para)   //noraml,normal1,high0....low0;
	{
		 total = 0;
		 int len = values.length<para.length?values.length:para.length;
		 for(int i = 0;i<values.length;i++)
		 {
			 values[i] = 0;
		 }
		 
		 for (int i = 0; i < len; i++) {
			 values[i] = para[i];
			 total += para[i];
		 }
		 
		 for(int i = 0; i< values.length; i++)
		 {
			 arcs[i] = 360 * (float)values[i]/(float)total;
		 }
		 
		 invalidate();   	
	}

	private void initSysPara()
	{
		DisplayMetrics dm = new DisplayMetrics();
		WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
		wm.getDefaultDisplay().getMetrics(dm);
		sysWidth = dm.widthPixels;
		sysHeight = dm.heightPixels;
	}
	
	public GluCakyView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		mContext = context;
		initSysPara();		
	}
	
    public GluCakyView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		mContext = context;
		
		initSysPara();;		
	}
	
	public GluCakyView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		mContext = context;
		
		initSysPara();	
	}    

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
		
		int widthMode = MeasureSpec.getMode(widthMeasureSpec);
		int widthSize = MeasureSpec.getSize(widthMeasureSpec);	
		int heightMode = MeasureSpec.getMode(heightMeasureSpec);
		int heightSize = MeasureSpec.getSize(heightMeasureSpec);
		int hSpec = heightMeasureSpec;
		int wSpec = widthMeasureSpec;
		

		
		if(widthMode == View.MeasureSpec.AT_MOST)
		{
			widthSize = (int) sysWidth/2-10;
			wSpec = View.MeasureSpec.makeMeasureSpec(widthSize, View.MeasureSpec.AT_MOST);
			hSpec = wSpec;
			super.onMeasure(wSpec, hSpec);	
		}		
		else
		super.onMeasure(wSpec, hSpec);
	}
	
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
	}
	
	@Override
	protected void onDraw(Canvas canvas){
		super.onDraw(canvas);
		
		int cy = this.getHeight() * 3 / 7;
		int cx = this.getWidth() * 3 / 7;
        int r = cx<cy?cx:cy;
        int thick = r/2+r/10;
		r = (r-thick)+thick/2;
		
		Paint mPaint = new Paint();
		mPaint.setStyle(Paint.Style.STROKE);
		//mPaint.setColor(GREEN);
		//mPaint.setStrokeJoin(Paint.Join.ROUND);   
		//mPaint.setStrokeCap(Paint.Cap.ROUND);   
		mPaint.setStrokeWidth(thick);   		
		mPaint.setAntiAlias(true);  
		
		RectF r1 = new RectF();
		
    	r1.left = cx - r;
    	r1.top = cy - r;
    	r1.right = cx + r;
    	r1.bottom = cy + r;	
    	
    	if(total == 0)
    	{
           mPaint.setColor(GREEN);
     	   canvas.drawArc(r1, 0, 360, false, mPaint);	    		
    	}
    	
        float start = 0.0f - arcs[0]/2;
 	   
        for(int i=0;i<values.length;i++)
        {       	
           mPaint.setColor(colors[i]);
    	   canvas.drawArc(r1, start, arcs[i], false, mPaint);	
    	   start += arcs[i];   	   
        }
        
         start = 0.0f - arcs[0]/2;
 	     int l1 = r-thick/2;
 	     int l2 = r+thick/2;    
  	     mPaint.setColor(Color.WHITE);
  	     mPaint.setStrokeWidth(3); 
        for(int i=0;i<values.length;i++)
        {       	
           if(arcs[i] > 3)
           {
           	   float x,y,x2,y2;
        	   
       		   x = (float) ((l1) * Math.cos(Math.toRadians(start)))+cx;
       		   y = (float) ((l1) * Math.sin(Math.toRadians(start)))+cy;   
       		
       		   x2 = (float) ((l2) * Math.cos(Math.toRadians(start)))+cx;
       		   y2 = (float) ((l2) * Math.sin(Math.toRadians(start)))+cy;         	   
       	       canvas.drawLine(x, y, x2, y2, mPaint);   	   
           }
           start += arcs[i];  
        } 
        
        start = 0.0f - arcs[0]/2;
        
        float fontSize = (thick - 10)/4;
		mPaint.setTextSize(fontSize);
		mPaint.setStyle(Paint.Style.FILL);
		mPaint.setColor(mtextColor); 
		int strID[] = {R.string.bp_measure_normal_per,R.string.bp_measure_norhigh_per};
        for(int i=0;i<2;i++)
        {
        	
        	float percent = (float)values[i]/(float)total;
        	percent = (float)(Math.round(percent*1000))/1000;
        	percent = percent*100;
        	String s = getResources().getString(strID[i]);       	
            start += arcs[i]/2;  
            
            float x,y,x1,y1,x2,y2;            
			float strW = mPaint.measureText(s);
			
    		x = (float) ((r) * Math.cos(Math.toRadians(start)))+cx;
    		y = (float) ((r) * Math.sin(Math.toRadians(start)))+cy;     		
    		x1 = x - strW/2;
    		y1 = y;
    		if(arcs[i] > 0)
    			canvas.drawText(s, x1, y1, mPaint);
    		
    		DecimalFormat decimalFormat=new DecimalFormat(".0");
			decimalFormat.setRoundingMode(RoundingMode.HALF_UP);

    		s= decimalFormat.format(percent) + "%";
    		strW = mPaint.measureText(s);
    		x2 = x - strW/2;
    		y2 = y + fontSize;
    		if(arcs[i] > 0)
    			canvas.drawText(s, x2, y2, mPaint);
    		
    		start += arcs[i]/2; 
        }         
 
	}	
		
}